import request from "@/plugins/request.js";
const api = request.api;

import log from "@/plugins/log.js";
let $log = log.logjson;



let query_user_permission = async function(permission_callback) {
	let roleId = localStorage.getItem("roleId");
	
	
	

	let res_menu = await api({
		url: '/admin_service.php',
		method: 'get',
		data: {
			action: "auth_getMenuList",
			page: 1,
			pageNum: 1000
		}
	})
	let menu_tree_cates = res_menu.data.list;
	let menu_flat_cates = getFlatFromTree(menu_tree_cates)

	$log('菜单配置项', menu_flat_cates)


	api({
		url: '/admin_service.php',
		method: 'get',
		data: {
			action: "auth_getRoleInfo",
			id: roleId
		}
	}).then(res => {
		if(res.code != 200) {
			permission_callback({
				status: 'error',
			})
		}
		
		if (res.code == 200) {
			let data = res.data;
			let roles = data.roles;
			if (roles) {
				let finally_id_list = roles.split(',')
				let id_values = []
				let auth_menu_list = []
				finally_id_list.forEach(menu_id => {
					let target_menu_item = menu_flat_cates.find((menu_item) => menu_item.id == menu_id);
					if (target_menu_item) {
						auth_menu_list.push(target_menu_item)
					}
				})
				$log('菜单访问权限 auth_menu_list', auth_menu_list)

				//匹配的菜单将所有目标菜单与子菜单路径拿到
				let flat_auth_menu_list = getAuthMenuFromTree(auth_menu_list);
				$log('菜单访问权限 flat_auth_menu_list', flat_auth_menu_list);

				//处理只勾选子级菜单的情况
				auth_menu_list.forEach(menu_item => {
					// 如果当前菜单只是子菜单 并且祖先菜单未开启权限  需要把祖先菜单权限开启  例如 【商品管理】-【商品列表】  勾选了[商品列表]  未勾选[商品管理] 需要开启商品管理权限
					if (menu_item.id_path != 1) {
						menu_item.id_path.forEach(parent_id => {
							if (parent_id != menu_item.id) {
								let parent_menu_item = menu_flat_cates.find((menu_item) => menu_item.id ==
									parent_id);
								if (parent_menu_item) {
									flat_auth_menu_list.push(parent_menu_item);
								}
							}
						})
					}
				})


				if (flat_auth_menu_list.length) {
					localStorage.setItem('is_limit_auth_route', 1)

					let auth_routes_map = {}
					flat_auth_menu_list.forEach(v => {
						let route = '/' + v.routeAddress
						auth_routes_map[route] = 1;
					})
					localStorage.setItem('auth_routes_map', JSON.stringify(auth_routes_map))
					// $router.push("/" + flat_auth_menu_list[0].routeAddress);
					// $store.commit('set_vuex_auth_menu_list', flat_auth_menu_list)


					if (permission_callback) {
						permission_callback({
							status: 'success',
							flat_auth_menu_list: flat_auth_menu_list
						})
					}

				} else {
					alert('当前账号无访问权限')
				}
			}
		}
	})
}




let getFlatFromTree = function(treeList, flat_list = [], parent_id_path = []) {
	treeList.forEach(v => {
		v.id_path = [...parent_id_path, v.id]
		flat_list.push(v);
		if (v.child && v.child.length) {
			// flat_list.push(...v.child)
			getFlatFromTree(v.child, flat_list, v.id_path)
		}
	})

	return flat_list
}



//授权菜单列表 获取一层菜单列表
let getAuthMenuFromTree = function(treeList, flat_list = []) {
	treeList.forEach(v => {
		flat_list.push(v);
		if (v.child && v.child.length) {
			getAuthMenuFromTree(v.child, flat_list)
		}
	})
	return flat_list
}


export default {
	query_user_permission
};